package ht_birds;

import hypertree.HyperTree;
import hypertree.SwingHTView;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;

import ui.mainWindow;

import dao.*;
/**
 * The Demo class implements a demo for HyperTree. It display an HyperTree view
 * of a demo tree. The demo tree is build from a file tree, passed in parameter.
 * Demo could take an argument, the path from which start the representing of
 * files. If no arguments is given, the treemap start from the root.
 */
public class hyper_birdsCountry {

	private static HTDBNode root = null; // the root of the demo tree
	private static HyperTree hypertree = null; // the hypertree builded

	/**
	 * Display a demo HyperTree.
	 * 
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		String pathRoot = null;

		if (args.length > 0) {
			pathRoot = args[0];
		} else {
			pathRoot = "birds";
		}

	//	try {
	//		ConnectionManager.connect(mainWindow.BD_NAME);
	//	} catch (SQLException ex) {
	//		System.out.println("Error while connecting to db: " + ex);
	//	} catch (ClassNotFoundException ex) {
	//		System.out.println("Class not found, check classpath");
	//	}

		
		//TODO nao retorna os valores corretamente fiz oq ele deveria retornar
		// Vector<BirdVO> bv = BirdDAO.arbitrarySQL("select risk_of_extinction from bird");
		Vector<String>bv = new Vector<String>();
	/*	bv.add(new BirdVO (null, "Least Concern", null, 0, null, 0, 0, null, 0, 0, null));
		bv.add(new BirdVO (null, "Near Threatened", null, 0, null, 0, 0, null, 0, 0, null));
		bv.add(new BirdVO (null, "Vulnerable", null, 0, null, 0, 0, null, 0, 0, null));*/
		
		root = new HTDBNode("birds");
		
		PreparedStatement comandoSelect = ConnectionManager.getConnection().prepareStatement("SELECT DISTINCT country_name FROM country");
		ResultSet resultados = comandoSelect.executeQuery();
		while(resultados.next())
		{
		
			String country = resultados.getString(1);
			
			Vector<CountryVO> cv = CountryDAO.selectByCountry(country);
			System.out.println("---"+country +": " + cv.size());
					
				if (cv.size() > 0) {
				
				HTDBNode child = new HTDBNode(country);
				
				for (int i = 0; i < cv.size(); i++) {

					HTDBNode grandson = new HTDBNode( BirdDAO.findByScientificName(cv.get(i).getBird_scientific_name()));
					child.addChild(grandson);
					System.out.println(grandson.getName());
				}
				
				root.addChild(child);

			}

			
		}

		
		
		//try {
	//		ConnectionManager.disconnect();
	//	} catch (SQLException e) {
	//		e.printStackTrace();
	//	}

		hypertree = new HyperTree(root);
		SwingHTView view = hypertree.getView();
		// HTNode firstchild = (HTNode) root.children().next();
		// view.translateToOrigin(firstchild);
		JFrame viewFrame = new JFrame("Birds HyperTree");
		viewFrame.setContentPane(view);
		viewFrame.setSize(500, 500);
		// viewFrame.pack();
		viewFrame.setVisible(true);
	}

}
